<?php
namespace member\service;

use herosphp\core\Loader;
use herosphp\bean\Beans;

class ReceiptAddressService extends CommonService{

    private $addressModel = null;

    public function __construct($model){

        parent::__construct($model);
        $this->addressModel = Loader::model('memberReceiptAddress');
    }

    public function getAddress($conditions){
        if(isset($conditions['s_phone_no'])){//运营商添加收货地址
            $memberService = Beans::get('member.member.service');
            $member_info = $memberService->getIdByMobile($conditions['s_phone_no']);
            if(!$member_info) return -1;
            unset($conditions['s_phone_no']);
            $conditions['member_id'] = $member_info['id'];
        }
        $conditions['app_id'] = appId();
        $items = $this->addressModel->getItems($conditions);
        if(!$items) return array('member_id'=>$conditions['member_id']);

        return $items;
    }

    public function addAddress($data,$isreturn=false){
        $addresstModel = $this->addressModel;
        if(isset($data['member_id'])){

        }
        if($data['add_type']==$addresstModel::ADD_TYPE_USER){//用户不能添加超过是个收货地址
            $items = $this->count(array('member_id'=>$data['member_id'],'app_id'=>appId(),'add_type'=>0));
            if($items>10) return -1;
        }
        $id = $addresstModel->add($data);
        if($isreturn){//运营商返回添加的收货信息
            return $id?$addresstModel->getItem($id):false;
        }else{
            return $id;
        }
    }

    public function add($data){
        if( empty($data['app_id']) ){
            $data['app_id'] = appId();
        }
        return parent::add($data);
    }
}

